package jsyf.oa.dao.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.device.DeviceServiceEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by y on 2018/4/16.
 */
@Repository
public class DeviceServiceDao extends BaseDao {
    public DeviceServiceDao() {
        super.iBaseEntity = new DeviceServiceEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, DeviceServiceEntity.class);
    }
    public List<JSONObject> getJSONListByPage1(String condition, Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ");
        sql.append(" t_dvc_service.ID AS id, ");
        sql.append(" t_dvc_service.FK_COMPANY_ID AS fkCompanyId, ");
        sql.append(" t_dvc_service.COMPANY_NAME AS companyName, ");
        sql.append(" t_dvc_service.BRAND_NAME AS brandName,");
        sql.append(" t_dvc_service.FK_BRAND_ID AS fkBrandId,");
        sql.append(" t_dvc_service.FK_MODEL_ID AS fkModelId,");
        sql.append(" t_dvc_service.MODEL_NAME AS modelName, ");
        sql.append(" t_dvc_service.FK_DEVICE_TYPE_ID AS fkDeviceTypeId, ");
        sql.append(" t_dvc_service.DEVICE_TYPE_NAME AS deviceTypeName,");
        sql.append(" t_dvc_service.SEQUENCE AS sequence,");
        sql.append(" t_dvc_service.SALED_TYPE AS saledType,");
        sql.append(" t_dvc_service.DELIVERY_DATE as deliveryDate,");
        sql.append(" t_dvc_service.YEARS AS years,");
        sql.append(" t_dvc_service.HOURS AS hours,");
        sql.append(" t_dvc_service.CUSTOMER_NAME AS customerName,");
        sql.append(" t_dvc_service.CUSTOMER_TEL AS customerTel,");
        sql.append(" t_dvc_service.ADMIN_NAME AS adminName,");
        sql.append(" t_dvc_service.ADMIN_TEL AS adminTel,");
        sql.append(" t_dvc_service.OPERATOR_NAME AS operatorName,");
        sql.append(" t_dvc_service.OPERATOR_TEL AS operatorTel,");
        sql.append(" t_dvc_service.REMARK AS remark,");
        sql.append(" t_dvc_service.CONFIGURATION AS configuration,");
        sql.append(" CASE WHEN t_dvc_service.DELIVERY_DATE LIKE '%-%' THEN 1 ELSE 0 END AS sortNo");
        sql.append(" FROM ");
        sql.append(iBaseEntity.getTableName());
        sql.append(" LEFT JOIN t_sys_company ON t_dvc_service.FK_COMPANY_ID=t_sys_company.ID");
        sql.append(" LEFT JOIN t_dvc_brand ON t_dvc_service.BRAND_NAME=t_dvc_brand.BRAND_NAME");
        sql.append(" LEFT JOIN t_dvc_model ON t_dvc_service.FK_MODEL_ID=t_dvc_model.ID");
        sql.append(" LEFT JOIN t_dvc_device_type ON t_dvc_service.DEVICE_TYPE_NAME=t_dvc_device_type.`NAME`");
        sql.append(" WHERE 1=1 AND t_dvc_service.DEL_FLAG=0");
        sql.append(condition);
        sql.append(" ORDER BY sortNo DESC,DELIVERY_DATE DESC ,COMPANY_NAME");
        super.appendPageSql(sql, requestPage, pageSize);
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);
        return jsonList;
    }
    public Integer getPageCount1(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT COUNT(1) AS count FROM " + iBaseEntity.getTableName() + "");
        sql.append(" LEFT JOIN t_sys_company ON t_dvc_service.FK_COMPANY_ID=t_sys_company.ID");
        sql.append(" LEFT JOIN t_dvc_brand ON t_dvc_service.BRAND_NAME=t_dvc_brand.BRAND_NAME");
        sql.append(" LEFT JOIN t_dvc_model ON t_dvc_service.FK_MODEL_ID=t_dvc_model.ID");
        sql.append(" LEFT JOIN t_dvc_device_type ON t_dvc_service.DEVICE_TYPE_NAME=t_dvc_device_type.`NAME`");
        sql.append(" WHERE 1=1 AND t_dvc_service.DEL_FLAG=0");
        sql.append(condition);
        String count = super.queryForString(sql.toString(), params);
        return Integer.parseInt(count);
    }
    public JSONObject getJSONById(String id) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ");
        sql.append(" t_dvc_service.ID AS id, ");
        sql.append(" t_dvc_service.COMPANY_NAME AS companyName, ");
        sql.append(" t_dvc_service.FK_COMPANY_ID AS fkCompanyId, ");
        sql.append(" t_dvc_service.BRAND_NAME AS brandName,");
        sql.append(" t_dvc_service.FK_BRAND_ID AS fkBrandId,");
        sql.append(" t_dvc_service.FK_MODEL_ID AS fkModelId,");
        sql.append(" t_dvc_service.MODEL_NAME AS modelName, ");
        sql.append(" t_dvc_service.FK_DEVICE_TYPE_ID AS fkDeviceTypeId, ");
        sql.append(" t_dvc_service.DEVICE_TYPE_NAME AS deviceTypeName,");
        sql.append(" t_dvc_service.SEQUENCE AS sequence,");
        sql.append(" t_dvc_service.SALED_TYPE AS saledType,");
        sql.append(" t_dvc_service.DELIVERY_DATE as deliveryDate,");
        sql.append(" t_dvc_service.YEARS AS years,");
        sql.append(" t_dvc_service.HOURS AS hours,");
        sql.append(" t_dvc_service.CUSTOMER_NAME AS customerName,");
        sql.append(" t_dvc_service.CUSTOMER_TEL AS customerTel,");
        sql.append(" t_dvc_service.ADMIN_NAME AS adminName,");
        sql.append(" t_dvc_service.ADMIN_TEL AS adminTel,");
        sql.append(" t_dvc_service.OPERATOR_NAME AS operatorName,");
        sql.append(" t_dvc_service.OPERATOR_TEL AS operatorTel,");
        sql.append(" t_dvc_service.CONFIGURATION AS configuration,");
        sql.append(" t_dvc_service.REMARK AS remark");
        sql.append(" FROM ");
        sql.append(iBaseEntity.getTableName());
        sql.append(" LEFT JOIN t_sys_company ON t_dvc_service.FK_COMPANY_ID=t_sys_company.ID");
        sql.append(" WHERE t_dvc_service.DEL_FLAG=0 AND t_dvc_service.ID=?");
        return super.queryForJsonObject(sql.toString(),new Object[]{id});
    }
    /*public List<JSONObject> findCompanyList(){
        StringBuffer condition = new StringBuffer();
        condition.append("SELECT");
        condition.append(" ID AS id,");
        condition.append(" COMPANY_NAME AS name");
        condition.append(" FROM t_dvc_service ");
        condition.append(" WHERE DEL_FLAG=0 GROUP BY COMPANY_NAME");
        return  queryForJsonList(condition.toString());
    }*/
}
